package kr.hkit.androidcomplete03.ch20;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MultiPaneWitdh extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multipanewidth);
	}
	
	public static class PlanetListFragment extends ListFragment{
		int mLastIndex;	//	마지막으로 선택한 값
		boolean mMultiPane;		// true면 태블릿
		
		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			setListAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, PlanetInfo.PLANET));
			getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			
			if(savedInstanceState != null){
				mLastIndex = savedInstanceState.getInt("lastIndex");
			}
			
			View descPanel = getActivity().findViewById(R.id.descpanel);
			
			// 현재 보여지거나 태블릿 모드가 생성되었는가
			if(descPanel != null && descPanel.getVisibility() == View.VISIBLE){
				// 태블릿
				mMultiPane = true;
				onListItemClick(getListView(), null, mLastIndex, 0);
			}
		}	// end of method onActivityCreated

		// 저장된 값을 불러옴
		@Override
		public void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
			outState.putInt("lastIndex", mLastIndex);
		}
				
		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			super.onListItemClick(l, v, position, id);
			
			mLastIndex = position;
			getListView().setItemChecked(position, true);
			
			if(mMultiPane){
				// 태블릿
				FragmentManager fm = getFragmentManager();
				DescFragment df = (DescFragment) fm.findFragmentById(R.id.descpanel);
				
				// 처음 생길 때와 기존에 있는 index와 선택한 index가 다를 떄 변환해줌.
				if(df == null || df.mNowIndex != position){
					df = DescFragment.newInstance(position);
					fm.beginTransaction().replace(R.id.descpanel, df).commit();
				}
				
			} else{
				// 핸드셋
				Intent intent = new Intent(getActivity(), DescActivity.class);
				intent.putExtra("index", position);		// 선택한 값이 넘어감.
				startActivity(intent);
			}
		} // end of method onListItemClick
	
	} // end of class PlanetListFragment
	
	public static class DescFragment extends Fragment{
		int mNowIndex;
		
		public static DescFragment newInstance(int index) {
			DescFragment df = new DescFragment();
			df.mNowIndex = index;
			return df;
		}
		
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View root = inflater.inflate(R.layout.descfragment, container, false);
			TextView tv = (TextView) root.findViewById(R.id.txtdescription);
			tv.setText(PlanetInfo.DESC[mNowIndex]);
			return root;
		}
		
	} // end of class DescFragment
	
	// 핸드셋일 경우
	public static class DescActivity extends Activity{
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			int index = getIntent().getExtras().getInt("index");
			DescFragment details = DescFragment.newInstance(index);
			getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
			
		}
	} // end of class DescActivity
}
